package com.redlight.base;

import com.google.common.collect.Maps;
import com.redlight.utils.StringUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * Created by chao on 2019/1/9.
 */
public class UserUtils {
    public static final String USER_CACHE_ID_ = "id_";
    public static final String USER_CACHE_LOGIN_NAME_ = "ln";
    public static final String USER_CACHE_loginFailMap_ = "loginFailMap_";
//    private static SysUserBaseService sysUserBaseService = (SysUserBaseService)ContextHolder.getBean(SysUserBaseService.class);
//    private static SysRoleBaseService sysRoleBaseService = (SysRoleBaseService)ContextHolder.getBean(SysRoleBaseService.class);

    public UserUtils() {
    }

    public static SysUser getUserById(String id) {
        SysUser user = (SysUser)EhCacheUtils.get("Eh_userCache", "id_" + id);
        if (user == null) {
            String adminApiDomain = Global.getConfig("adminApiDomain");

            try {
//                user = sysUserBaseService.getUser(id, adminApiDomain);
            } catch (Exception var5) {
                var5.printStackTrace();
            }

            if (user == null) {
                return null;
            }

            try {
//                user.setRoleList(sysRoleBaseService.getUserRoleList(id, adminApiDomain));
            } catch (Exception var4) {
                var4.printStackTrace();
            }

            EhCacheUtils.put("Eh_userCache", "id_" + user.getId(), user);
            EhCacheUtils.put("Eh_userCache", "ln" + user.getLoginName(), user);
        }

        return user;
    }

    public static SysUser getByLoginName(String loginName) {
        SysUser user = new SysUser();
        user.setLoginName(loginName);
        return user;
    }

    public static boolean isValidateCodeLogin(String useruame, boolean isFail, boolean clean) {
        Map<String, Integer> loginFailMap = (Map)EhCacheUtils.get("Eh_userCache", "loginFailMap_" + useruame);
        if (loginFailMap == null) {
            loginFailMap = Maps.newHashMap();
            EhCacheUtils.put("Eh_userCache", "loginFailMap_" + useruame, loginFailMap);
        }

        Integer loginFailNum = (Integer)((Map)loginFailMap).get(useruame);
        if (loginFailNum == null) {
            loginFailNum = 0;
        }

        if (isFail) {
            loginFailNum = loginFailNum + 1;
            ((Map)loginFailMap).put(useruame, loginFailNum);
        }

        if (clean) {
            ((Map)loginFailMap).remove(useruame);
        }

        return loginFailNum >= 3;
    }

    public static void removeCache(HttpServletRequest request, String userId) {
        HttpSession session = request.getSession();
        session.removeAttribute("SubjectId");
        session.removeAttribute("Authorization");
        session.removeAttribute("user");
        if (StringUtil.isNotBlank(userId)) {
            EhCacheUtils.remove("Eh_userCache", "id_" + userId);
        }

    }
}
